<template>
  <div class="contacts-main">
    <div class="contacts-lst">
      <ele-empty v-if="selectDept.length==0" text="暂无联系人"></ele-empty>
      <template v-else>
        <div class="nav">
          <div @click="deptItemCk(item)" v-for="(item,index) in selectDept">
            <span class="name ele-text-primary" v-if="item.parent_id==0 && selectDept.length<3">{{item.dept_full_name}}</span>
            <span class="name ele-text-primary" v-else>{{item.dept_name}}</span>
            <span style="padding:0 5px;" v-if="index!=(selectDept.length-1)">></span>
          </div>
        </div>
        <div class="dept-lst">
          <div @click="deptItemCk(item)" class="dept-item" v-for="(item) in selectDept[selectDept.length-1].child">
            <i class="el-icon-circle-plus-outline"></i>
            <span class="name">{{item.dept_name}}</span>
          </div>
        </div>
        <div class="user-lst">
          <div @click="toChat(item.user_id,item.username,item.realname,item.avatar)" class="user-item" v-for="(item) in selectDept[selectDept.length-1].user">
            <el-avatar :size="25" :src="item.avatar+'_50'"/>
            <span class="name">{{item.realname || item.username}}</span>
          </div>
        </div>
        <ele-empty v-if="(!selectDept[selectDept.length-1].child || selectDept[selectDept.length-1].child.length==0) && selectDept[selectDept.length-1].user.length==0" text="暂无成员"></ele-empty>
      </template>
    </div>
    <div class="contacts-detail"></div>
  </div>
</template>

<script>
import { mapState,mapMutations } from 'vuex';
import util from '@/utils/util';
let me = null;
export default {
  name: 'contacts',
  components: {},
  computed: {
    ...mapState({
      historyMsgList: ({chatModule}) => chatModule.historyMsgList,
      userInfo: ({user}) => user.user,
    })
  },
  data() {
    return {
      pageData:[],
      selectDept:[]
    };
  },
  created(){
    me = this;
    this.query();
  },
  mounted() {
  },
  methods: {
    ...mapMutations(['chatModule/setChatTo', 'chatModule/setChatWindowsId', 'baseModule/setMainTab', 'chatModule/setHistoryMsgList']),
    query() {
      this.$http.post('/app.user.dept.tree').then(res => {
        this.loading = false;
        if (res.data.code === 0) {
          this.pageData = res.data.data;
          this.selectDept = res.data.data;
        } else {
          this.$message.error(res.data.msg || '获取数据失败');
        }
      }).catch(e => {
        this.loading = false;
        this.$message.error(e.message);
      });
    },
    deptItemCk(obj){
      var index = me.selectDept.findIndex(d => d.dept_id === obj.dept_id);
      // console.log(index);
      if(index==-1){
        me.selectDept.push(obj);
      }
      else if(index<(me.selectDept.length-1)){
        me.selectDept = me.selectDept.slice(0,index+1);
      }
    },
    toChat(id,name,realname,avatar){
      if(id==this.userInfo.user_id){
        return false;
      }
      var newHistoryMsgList = {};
      var local = {};
      if (this.historyMsgList != null && this.historyMsgList != '') {
        if (this.historyMsgList[this.userInfo.user_id] != null && this.historyMsgList[this.userInfo.user_id] != '') {
          //有列表
          local = this.historyMsgList[this.userInfo.user_id]['friend'+id] || {};
        }
      }
      if(JSON.stringify(local) == "{}"){
        //添加空消息
        var chatlog = local.chatlog || [];
        var history = [{"name":name,"realname":realname,"avatar":avatar,"id":id,"type":"friend","content":"","timestamp":new Date().getTime()}];
        local.history = history;
        local.chatlog = chatlog;
        newHistoryMsgList = util.deepCopy(this.historyMsgList || {});
        var myHistoryMsg = newHistoryMsgList[this.userInfo.user_id] || {};
        myHistoryMsg['friend'+id]=local;
        newHistoryMsgList[me.userInfo.user_id] = myHistoryMsg;
        me['chatModule/setHistoryMsgList'](newHistoryMsgList);
        me.setStore('historyMsgList',newHistoryMsgList);
      }


      var to = {};
      to.id=id;
      to.name=name;
      to.realname = realname;
      to.avatar=avatar;
      to.unread = 0;
      to.type = 'friend';
      me['chatModule/setChatTo'](to);
      me['chatModule/setChatWindowsId'](id);
      me['baseModule/setMainTab']('chat');
    }
  }
}
</script>

<style scoped>
.contacts-main{
  display:flex;
}
.contacts-lst{
  width:256px;
  background:#fff;
  position: relative;
  height: calc(100vh - 46px);
}
.contacts-detail{
  flex:1;
  display: flex;
  flex-direction: column;
}
.nav{
  height: 45px;
  line-height: 45px;
  display: flex;
  padding:0 10px;
  position:relative;
}
.nav .name{
  cursor: pointer;
}
.dept-item,.user-item{
  height: 45px;
  display: flex;
  align-items: center;
  position:relative;
  padding-left: 10px;
  cursor: pointer;
}
.dept-item:hover,.user-item:hover{
  background:#E0E2E4;
}
.dept-item .name,.user-item .name{
  margin-left: 5px;
}
.dept-item .name{
  font-weight:bold;
}
.dept-item:after,.user-item:after{
  position: absolute;
  background-image: url();
  top: 12px;
  right: 10px;
  bottom: 0;
  display: block;
  width: 15px;
  height: 15px;
  color: #8799a3;
  content: "";
  text-align: center;
  font-size: 17px;
  line-height: 15px;
  background-size: cover;
}
.nav:after,
.dept-item:before,.user-item:before{
  content: ' ';
  position: absolute;
  left: 0;
  bottom: 0;
  right: auto;
  top: auto;
  height: 1px;
  background-color: #ccc;
  width: 100%;
  transform: scaleY(0.25);
  transform-origin: 0 75%;
  pointer-events: none;
}
</style>
